package com.pwr.app.ui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import com.pwr.app.TestType;
import com.pwr.app.characteristics.CharacteristicsEnum;
import com.pwr.app.classification.ClassificationEnum;

public class TestingWindow extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;
	JDialog jDialog;
	String[] options = { "10", "20", "30", "40", "50", "100", "150", "200", "500", "1000" };
	JCheckBox fourier = new JCheckBox("Fourier");
	JCheckBox hisotgram = new JCheckBox("Histogram");
	JCheckBox density = new JCheckBox("Density");
	JCheckBox huMoments = new JCheckBox("Hu moments");
	JRadioButton effectivness = new JRadioButton("Characteristic testing");
	JRadioButton vectorLenght = new JRadioButton("Vector lenght testing");
	JRadioButton mlp = new JRadioButton("MLP");
	JRadioButton knn = new JRadioButton("k-NN");
	JButton okButton = new JButton("OK");
	JButton cancelButton = new JButton("Cancel");
	JScrollPane listScroller;
	private Map<CharacteristicsEnum, Boolean> characteristicsUsed;
	private TestType testType;
	private ClassificationEnum chosenClassificator;
	private int iterations;

	public int getIterations() {
		return iterations;
	}

	public void setIterations(int iterations) {
		this.iterations = iterations;
	}

	public TestingWindow(JFrame parent, String title, String message, TestType testType, Map<CharacteristicsEnum, Boolean> characteristicsUsed, ClassificationEnum chosenClassificator) {
		super(parent, title, true);
		Dimension parentSize = parent.getSize();
		Point p = parent.getLocation();
		this.setTestType(testType);
		this.setCharacteristicsUsed(characteristicsUsed);
		this.setChosenClassificator(chosenClassificator);
		setLocation(p.x + parentSize.width / 4, p.y + parentSize.height / 4);
		CreateAndShowGUI(parent, title);
	}

	private void CreateAndShowGUI(JFrame parent, String title) {
		jDialog = new JDialog(parent, title); // Creates dialog
		jDialog.setModal(true); // Means it will wait
		jDialog.add(CreatePanel()); // Add your panel
		jDialog.pack(); // Set size (probably want this relating to your panel
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		jDialog.setLocation(dim.width / 2 - jDialog.getWidth() / 2, dim.height / 2 - jDialog.getHeight() / 2);
		jDialog.setVisible(true);
		jDialog.setResizable(false);
		jDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

	}

	private void exit() {
		jDialog.dispose();
	}

	private JPanel CreatePanel() {
		JPanel TotalGUI = new JPanel(new GridLayout(4, 2));

		JPanel radioButtonPanel = new JPanel(new GridLayout(2, 2));
		JPanel testingButtonPanel = new JPanel(new GridLayout(1, 2));
		JPanel classificatorButtonPanel = new JPanel(new GridLayout(1, 2));

		fourier.addActionListener(this);
		hisotgram.addActionListener(this);
		density.addActionListener(this);
		huMoments.addActionListener(this);
		fourier.setSelected(true);
		radioButtonPanel.add(fourier);
		radioButtonPanel.add(hisotgram);
		radioButtonPanel.add(density);
		radioButtonPanel.add(huMoments);

		vectorLenght.addActionListener(this);
		effectivness.addActionListener(this);
		testingButtonPanel.add(vectorLenght);
		testingButtonPanel.add(effectivness);

		mlp.addActionListener(this);
		knn.addActionListener(this);
		classificatorButtonPanel.add(mlp);
		classificatorButtonPanel.add(knn);

		okButton.addActionListener(this);
		cancelButton.addActionListener(this);
		JTextField field = new JTextField("Classifier Type:");
		field.setEditable(false);
		TotalGUI.add(field);
		TotalGUI.add(classificatorButtonPanel);
		field = new JTextField("Test Type:");
		field.setEditable(false);
		TotalGUI.add(field);
		TotalGUI.add(testingButtonPanel);
		field = new JTextField("Characteristisc Types:");
		field.setEditable(false);
		TotalGUI.add(field);
		TotalGUI.add(radioButtonPanel);
		TotalGUI.add(okButton);
		TotalGUI.add(cancelButton);

		return TotalGUI;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();

		if (source == density) {
			getCharacteristicsUsed().put(CharacteristicsEnum.Density, !getCharacteristicsUsed().get(CharacteristicsEnum.Density));
		}
		if (source == fourier) {
			getCharacteristicsUsed().put(CharacteristicsEnum.Fourier, !getCharacteristicsUsed().get(CharacteristicsEnum.Fourier));

		} else if (source == hisotgram) {
			getCharacteristicsUsed().put(CharacteristicsEnum.Histogram, !getCharacteristicsUsed().get(CharacteristicsEnum.Histogram));

		} else if (source == huMoments) {
			getCharacteristicsUsed().put(CharacteristicsEnum.HuMoment, !getCharacteristicsUsed().get(CharacteristicsEnum.HuMoment));
		} else if (source == effectivness) {
			effectivness.setSelected(true);
			vectorLenght.setSelected(false);
			setTestType(TestType.Effectivness);
			huMoments.setEnabled(true);
		} else if (source == vectorLenght) {
			effectivness.setSelected(false);
			vectorLenght.setSelected(true);
			setTestType(TestType.VectorLenght);
			huMoments.setEnabled(false);
			getCharacteristicsUsed().put(CharacteristicsEnum.HuMoment, false);
		} else if (source == mlp) {
			mlp.setSelected(true);
			knn.setSelected(false);
			setChosenClassificator(ClassificationEnum.NeuralNet);
		} else if (source == knn) {
			mlp.setSelected(false);
			knn.setSelected(true);
			setChosenClassificator(ClassificationEnum.NearestNeighbour);
		} else if (source == okButton)
			exit();
		else if (source == cancelButton)
			exit();

	}

	public ClassificationEnum getChosenClassificator() {
		return chosenClassificator;
	}

	public void setChosenClassificator(ClassificationEnum chosenClassificator) {
		this.chosenClassificator = chosenClassificator;
	}

	public TestType getTestType() {
		return testType;
	}

	public void setTestType(TestType testType) {
		this.testType = testType;
	}

	public Map<CharacteristicsEnum, Boolean> getCharacteristicsUsed() {
		return characteristicsUsed;
	}

	public void setCharacteristicsUsed(Map<CharacteristicsEnum, Boolean> characteristicsUsed) {
		this.characteristicsUsed = characteristicsUsed;
	}

}
